﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Http;

namespace MSTutHelloWebAPI.Models
{
    public class CaseRepository : ICaseRepository
    {
        List<Case> caselist = new List<Case>();

        public CaseRepository()
        {
            caselist = DBHelper.GetAllCases();
        }

        public IEnumerable<Case> GetAll()
        {
            return caselist;
        }

        public IEnumerable<Case> GetByCaseOwner(string caseOwner)
        {
            caselist = DBHelper.GetCasesByCaseOwner(caseOwner);
            return caselist;
        }

        public Case Get(string id)
        {
            return caselist.Find(c => c.caseID == id);
        }

        public string GetImagePath(string id)
        {
            return DBHelper.GetCasesImage(id);
        }

        public Case Add(Case caseObj)
        {
            if (caseObj != null)
            {
                caseObj.lastUpdateDateTime = DateTime.Now.ToString();
                if (DBHelper.CreateCase(caseObj) != 0)
                {
                    caselist.Add(caseObj);
                }
            }
            else
            {
                throw new ArgumentNullException("caseObj");
            }
            //caseObj.caseID = Case.NewCaseID();
            
            return caseObj;
        }

        public void Remove(string id)
        {
            caselist.RemoveAll(c => c.caseID == id);
        }

        public UpdateCaseResponse Update(Case caseObj)
        {
            UpdateCaseResponse ucr = new UpdateCaseResponse();
            if (caseObj == null)
            {
                throw new ArgumentNullException("caseObj");
            }
            else
            {
                int index = caselist.FindIndex(c => c.caseID == caseObj.caseID);
                if (index == -1)
                {
                    ucr.Result = "Case updating failed!";
                    ucr.ErrorMessage = new HttpResponseException(HttpStatusCode.NotFound).ToString();
                    ucr.IsSuccess = false;
                }
                else
                {
                    if (DBHelper.UpdateCase(caseObj) == 0)
                    {
                        ucr.Result = "Case updating failed!";
                        ucr.ErrorMessage = new HttpResponseException(HttpStatusCode.NoContent).ToString();
                        ucr.IsSuccess = false;
                    }
                    else 
                    {
                        caselist.RemoveAt(index);
                        caselist.Add(caseObj);
                        ucr.Result = "Case updating successfully!";
                        ucr.IsSuccess = true;
                    }
                }
            }
            return ucr;
        }

        public bool UpdateCaseDesc(string caseID, string closureDesc, string caseStatus, string closedDate)
        {
            if (caseID == null)
            {
                throw new ArgumentNullException("caseID");
            }
            else if (closureDesc == null)
            {
                throw new ArgumentNullException("caseDesc");
            }
            int index = caselist.FindIndex(c => c.caseID == caseID);
            if (index == -1)
            {
                return false;
            }
            Case Case2bUpdated = caselist[index];
            Case2bUpdated.closureDesc = closureDesc;
            Case2bUpdated.caseStatus = caseStatus;
            Case2bUpdated.closedDate = closedDate;
            if (DBHelper.UpdateCaseDesc(Case2bUpdated))
            {
                caselist.RemoveAt(index);
                caselist.Add(Case2bUpdated);
                return true;
            }
            else
            {
                return false;
            }
            
        }
    }
}